package com.woniu.atms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.atms.entity.G5Route;
import com.woniu.atms.entity.G5Site;
import com.woniu.atms.mapper.RouteMapper;
import com.woniu.atms.mapper.SiteMapper;
import com.woniu.atms.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
  *@title RouteServiceImpl
  *@author LHY
  *@createTime 2022/1/18/17:34
 **/
@Service
public class RouteServiceImpl implements RouteService {
    @Resource
    private RouteMapper routeMapper;
    @Resource
    private SiteMapper siteMapper;

    /**
     * 按照id查询路线
     * @param id
     * @return
     */
    @Override
    public G5Route findRouteById(int id) {
        return routeMapper.findRouteById(id);
    }

    /**
     * 查询所有路线
     * @return
     */
    @Override
    public PageInfo<G5Route> findAllRoute(Integer pageNum) {
        //设置页面展示内容数，和页码
        PageHelper.startPage(pageNum,12);
        //获取集合
        List<G5Route> routes=routeMapper.findAllRoute();
        //获取PageInfo对象
        PageInfo<G5Route> pageInfo=new PageInfo<>(routes);
        return pageInfo;
    }

    /**
     * 根据路线状态查询路线
     * @param status
     * @return
     */
    @Override
    public PageInfo<G5Route> findRouteByStatus(String status,Integer pageNum) {
        PageHelper.startPage(pageNum,2);
        List<G5Route> routes=routeMapper.findRouteByStatus(status);
        PageInfo<G5Route> pageInfo=new PageInfo<>(routes);
        return pageInfo;
    }

    /**
     * 更新路线状态和删除路线
     * @param route
     * @return
     */
    @Override
    public boolean updateRoute(G5Route route) {
        return routeMapper.updateRoute(route);
    }

    /**
     * 根据输入的起点地址和终点地址查询路线
     * @param startSites 起始站点集合
     * @param endSites 终止站点集合
     * @return
     */
    @Override
    public List<G5Route> findRoutesByStarAndEndAddress(List<G5Site> startSites, List<G5Site> endSites) {
        List<Integer> startIds=new ArrayList<>();
        List<Integer> endIds=new ArrayList<>();
        for(G5Site r:startSites){
            startIds.add(r.getId());
        }
        for(G5Site r:endSites){
            endIds.add(r.getId());
        }
        return routeMapper.findRoutesByStarAndEndAddress(startIds,endIds);
    }

    /**
     * 添加路线
     * @param route
     * @return
     */
    @Override
    public boolean addRoute(G5Route route) {
        route.setStatus("y");
        route.setIsExit("y");
        return routeMapper.addRoute(route);
    }

    @Override
    public G5Route findRouteByName(String name) {
        return routeMapper.findRouteByName(name);
    }

}
